﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

///Developed By Indie Studio
///https://assetstore.unity.com/publishers/9268
///www.indiestd.com
///info@indiestd.com

namespace IndieStudio.BugsBang.Game
{
	[DisallowMultipleComponent]
	public class Paths : MonoBehaviour {

		/// <summary>
		/// A static instance of this class.
		/// </summary>
		public static Paths instance;

		/// <summary>
		/// The list of the path objects in the scene .
		/// </summary>
		public List<PathObject> pathObjectPool = new List<PathObject>();

		/// <summary>
		/// The list of paths in the scene.
		/// </summary>
		public List<Path> paths = new List<Path>();

		void Awake(){
			if(instance == null)
				instance = this;
		}

		// Use this for initialization
		void Start () {
		}

		/// <summary>
		/// Pause the paths.
		/// </summary>
		public void PausePaths(){
			foreach (PathObject po in Paths.instance.pathObjectPool) {
				po.Stop ();
			}
			foreach (Path path in paths) {
				path.SetPause (true);
			}
		}

		/// <summary>
		/// Resume the paths.
		/// </summary>
		public void ResumePaths(){
			foreach (PathObject po in Paths.instance.pathObjectPool) {
				po.Move ();
			}
			foreach (Path path in paths) {
				path.SetPause (false);
			}
		}

		/// <summary>
		/// Get any wanted path object nearby the center point of the screen.
		/// </summary>
		/// <returns>The wanted path object nearby the center.</returns>
		/// <param name="distance">The max distance between the object and the center.</param>
		public PathObject GetWantedPathObjectNearCenter(float distance){

			List<PathObject> poList = new List<PathObject> ();
			foreach(PathObject po in pathObjectPool){
				if (po.isTarget && Vector2.Distance(Vector3.zero,po.transform.position) <= Mathf.Abs(distance)) {
					poList.Add (po); 
				}
			}

			if (poList.Count == 0) {
				return null;
			}

			return poList[Random.Range (0, poList.Count)];
		}

		/// <summary>
		/// Swap the path objects in the given path with another path objects in a random path.
		/// </summary>
		/// <param name="path">Path.</param>
		public void SwapPathObjects(Path path){
			if (path == null) {
				return;
			}

			List<int> indexes = new List<int> ();

			for (int i = 0; i < paths.Count; i++) {
				if (!paths [i].allowSwap)
					continue;
				indexes.Add (i);
			}

			List<PathObject> tempPathObjects = path.pathObjects;
			Path randomPath = paths [Random.Range (0, indexes.Count)];

			randomPath.allowSwap = path.allowSwap = false;
			path.pathObjects = randomPath.pathObjects;
			randomPath.pathObjects = tempPathObjects;
		}

		/// <summary>
		/// Add path object to the pool.
		/// </summary>
		/// <param name="po">Path Object Reference.</param>
		public void AddToPool(PathObject po){
			if (po == null) {
				return;
			}
			pathObjectPool.Add (po);
		}

		/// <summary>
		/// Delete the path object by intance ID.
		/// </summary>
		/// <param name="intanceID">Intance I.</param>
		public void DeleteByIntanceID(int intanceID){
			for(int i = 0 ; i < pathObjectPool.Count;i++){
				if (pathObjectPool[i].GetInstanceID () == intanceID) {
					pathObjectPool.RemoveAt (i);
					break;
				}
			}
		}

		/// <summary>
		/// Clear the path object pool.
		/// </summary>
		public void ClearPathObjectPool(){
			pathObjectPool.Clear ();
		}
	}
}